<?php

namespace App\Manage;


use App\Models\Admin;
use App\Models\AdminRole;

class AdminRoleManage extends Manage
{

    // 角色

    // 修改

    // 删除

    // 模型
    protected function getModel()
    {
        return new AdminRole();
    }

    // 获取菜单
    public function menu()
    {
        $menus = (new Admin())->getAllMenu();

        return $menus;
    }


    // 整理列表数据
    protected function _afterList($list)
    {
        if(empty($list)){
            return $list;
        }

        $menus = (new Admin())->getAllMenu();
        $menus = array_column($menus, null, 'role');
        foreach($list as &$_row){
            // 权限信息
            $_row['auths']     = [];
            $_row['auth_name'] = $_row['en_auth_name'] = $comm = '';
            // 权限处理
            $auths = is_string($_row['auth']) ? json_decode($_row['auth'], true) : $_row['auth'];
            if(empty($auths)){
                continue;
            }

            // 如果有权限
            foreach($auths as $_auth){
                if(empty($menus[$_auth])){
                    continue;
                }

                $_row['auth_name']    .= $comm.$menus[$_auth]['name'];
                $_row['en_auth_name'] .= $comm.$menus[$_auth]['en_name'];
                $comm                 = ',';
            }
        }

        return $list;
    }

    // 添加
    protected function _before_add()
    {

        // 参数
        $is_admin_log = $options['is_admin_log'] ?? 0;


        $opt  = $this->option();
        $data = [
            'intro'  => $opt->getData('intro', null, 'trim'),
            'name'   => $opt->getData('name', null, 'trim'),
            'auth'   => $opt->getData('auths', null, 'is_array'),
            'status' => $opt->getData('status', 1),
        ];

        // 权限判断
        if(count($data['auth']) != count($data['auth'], 1)){
            //return $this->_returnFail('权限必须是一维数组！');
            return $this->_returnFail('permissions_must_one_dimensional_array');
        }
        // 权限处理
        if($data['auth']){
            $menus = (new Admin())->getAllMenu();
            $menus = array_column($menus, 'role');
            $auths = [];
            foreach($data['auth'] as $_auth){
                if(in_array($_auth, $menus)){
                    $auths[] = $_auth;
                }
            }

            $data['auth'] = json_encode($auths);
        }

        return $data;
    }

    // 修改
    protected function _before_edit()
    {

        // 参数
        $is_admin_log = $options['is_admin_log'] ?? 0;


        $opt  = $this->option();
        $data = [
            'intro'  => $opt->getData('intro', null, 'trim'),
            'name'   => $opt->getData('name', null, 'trim'),
            'auth'   => $opt->getData('auths', null, 'is_array'),
            'status' => $opt->getData('status', 1),
        ];

        // 权限判断
        if(count($data['auth']) != count($data['auth'], 1)){
            //return $this->_returnFail('权限必须是一维数组！');
            return $this->_returnFail('permissions_must_one_dimensional_array');
        }
        // 权限处理
        if($data['auth']){
            $menus = (new Admin())->getAllMenu();
            $menus = array_column($menus, 'role');
            $auths = [];
            foreach($data['auth'] as $_auth){
                if(in_array($_auth, $menus)){
                    $auths[] = $_auth;
                }
            }

            $data['auth'] = json_encode($auths);
        }


        return $data;
    }
}

